<?php

namespace App\Http\Controllers\Admin;

use App\Service\VipService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Ramsey\Uuid\Uuid;

class VipController extends Controller
{
    //
    private $vipService;

    public function __construct(VipService $vipService)
    {
        $this->vipService = $vipService;
    }

    public function getVipList(){
        $data = $this->vipService->getVipList();
        return view('vip_list')->with('data',$data);
    }

    public function getVipInfo(Request $request){
        $id = $request->get('id');
        $data =$this->vipService->getVipInfo($id)->toArray();
        return view('vip_info')->with('data',$data);
    }

    public function addVip(){
        return view('vip_add');
    }

    public function doAddVip(Request $request){
        $vip = $request->except('image');
        if($request->has('image')){
            $img = $request->file('image');
            $vip['image'] = $this->imgUpload($img);
        }
        $this->vipService->addVip($vip);
        return redirect('/getVipList');
    }
    public function imgUpload($file){

        if($file->isValid()){
            $mimetype = $file->getClientMimeType();
            $originalName = $file->getClientOriginalName();
            $ext = $file->getClientOriginalExtension();
            $realPath = $file->getRealPath();
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            $bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));
            $path = "/../public/uploads/";
            $url = '/uploads/'.$filename;
            return $url;
        }

    }

    public function updateVip(Request $request){
        $id = $request->get('id');
        $data = $this->vipService->getVipInfo($id);
        return view('vip_update')->with('vip',$data);
    }

    public function doUpdateVip(Request $request){
        $vip = $request->except('image','_token');
        $img = $request->file('image');
        if($img){
            $vip['image'] = $this->imgUpload($img);
        }

        $this->vipService->updateVip($vip);
        return redirect('/getVipList');
    }

    public function deleteVip(Request $request){
        $id = $request->get('id');
        $this->vipService->deleteVip($id);
        return redirect('/getVipList');
    }

    public function getDeleteVip(){
        $data = $this->vipService->getDeleteVip();
        return view('vip_delete_list')->with('data',$data);
    }

    public function recoveryVip(Request $request){
        $id = $request->get('id');
        $this->vipService->recoveryVip($id);
        return redirect('/getVipList');
    }

    public function getClassList(){
        $data  = $this->vipService->getClassList();
        return view('class_list')->with('data',$data);
    }
    public function updateClass(Request $request){
        $id = $request->get('id');
        $data = $this->vipService->getClass($id);
        return view('class_update')->with('class',$data);
    }

    public function doUpdateClass(Request $request){
        $data = $request->except('_token');
        $this->vipService->updateClass($data);
        return redirect('/getClassList');
    }
    public function addClass(Request $request){
        return view('class_add');
    }
    public function doAddClass(Request $request){
        $data = $request->except('_token');
        $this->vipService->addClass($data);
        return redirect('/getClassList');
    }
    public function deleteClass(Request $request){
        $id = $request->get('id');
        $this->vipService->deleteClass($id);
        return redirect('/getClassList');
    }

}
